﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ICSharpCode.SharpZipLib.Zip;
using COM.Web;
using ICSharpCode.SharpZipLib.GZip;
using WebApp.Web.Service;
using WebApp.Service;
using WebApp.Domain;
using COM.Data;
using COM.Service;

namespace WebApp.Web.Controllers
{
    public class UserInfoController : Controller
    {
        private IUploadFileService uploadFileService = Unity.Instance.GetService<IUploadFileService>();
        private IAppPathService appPathService = Unity.Instance.GetService<IAppPathService>();
        private IRuntimeService runtimeService = Unity.Instance.GetService<IRuntimeService>();
        private IUserService userService = Unity.Instance.GetService<IUserService>();

        public ActionResult Index()
        {
            return View("Index");
        }

        [HttpPost]
        public ActionResult UploadUserPic(Guid id, string fileName)
        {
            try
            {
                var fileID = id.ToString();
 
                if (Request.Files.Count != 1)
                    throw new ApplicationException("上传文件应有一个文件流.");

                HttpPostedFileBase postFile = Request.Files[0];
                if (postFile.ContentLength == 0)
                    throw new ApplicationException("空文件");

                var path = string.Empty;//Todo: this.uploadFileService.UploadUserFeedbackPath;
                //var path = this.uploadFileService.UploadPicturePath;
                var uploadFilePath = System.IO.Path.Combine(path, string.Format("{0}-{1}", fileID, DateTime.Now.ToString("yyyyMMdd-HHmmss-fff")) + System.IO.Path.GetExtension(fileName));
                postFile.SaveAs(uploadFilePath);

                var fileUrl = uploadFilePath.Substring(appPathService.AppDataPath.Length).Replace('\\', '/').TrimStart('/');
                var filePath = string.Format("file://{0}", fileUrl);
                this.userService.UpdateUserPicture(id, filePath);
                return Json(new { success = true }, "text/html");
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message }, "application/json");
            }

        }

        public ActionResult UserPicture(string id)
        {
            id = id.ToLower().Replace('$','.');
            Guid userID = Guid.Empty;
            string stringUserID = id.IndexOf('.') > 0?id.Substring(0, id.IndexOf('.')):id;
            Guid.TryParse(stringUserID, out userID);
            if (userID == Guid.Empty)
                return null;

            var user = this.userService.GetUserByID(userID);
            if (!user.UserPicFilePath.TrimOrEmpty().StartsWith("file://"))
                throw new ApplicationException("用户上传的头像文件以相对路径方式存储");

            var filePath = user.UserPicFilePath.TrimOrEmpty().Substring("file://".Length);
            var fileName = System.IO.Path.Combine(appPathService.AppDataPath, filePath);
            if (!System.IO.File.Exists(fileName))
            {
                this.runtimeService.LogService.LogInfo("fileName = {0}", fileName);
                var defaultPic = System.IO.Path.Combine(appPathService.AppDataPath, @"..\Content\npc\girl02.jpg");
                return File(defaultPic, "image/jpeg", "pic" + System.IO.Path.GetExtension(defaultPic));
            }
            return File(fileName, "image/jpeg", "pic" + System.IO.Path.GetExtension(fileName));
        }


    }
}
